package com.fancon.android.emoji.keyboard;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RadioButton;

import com.fancon.android.emoji.Emoji;
import com.fancon.android.emoji.EmojiDrawableGetter;
import com.fancon.android.emoji.EmojiKeyBoard.EmojiKeyBoardListener;
import com.fancon.android.emoji.EmojiUtil;
import com.fancon.android.emoji.EmojiUtil.EmojiMap;
import com.fancon.android.emoji.R;
import com.fancon.android.emoji.widget.IRichText;

public class M05KeyBoard extends BetterPopupWindow implements OnClickListener {
	private EmojiKeyBoardListener listener;
	private EmojiUtil mEmojiUtil;
	private RadioButton radioSmiley, radioBell, radioVehicle, radioFlower,
			radioNumber;
	private Boolean isFromTop = true;
	private int initCategory = 0;
	private ImageView close;

	public M05KeyBoard(View anchor, final EmojiKeyBoardListener listener) {
		super(anchor);
		this.listener = listener;
		loadEmoji(initCategory);
	}

	public M05KeyBoard(View anchor, Boolean isFromTop,
			final EmojiKeyBoardListener listener) {
		super(anchor);
		this.listener = listener;
		this.isFromTop = isFromTop;
		loadEmoji(initCategory);
	}

	public M05KeyBoard(View anchor, Boolean isFromTop, int initCategory,
			final EmojiKeyBoardListener listener) {
		super(anchor);
		this.listener = listener;
		this.isFromTop = isFromTop;
		this.initCategory = initCategory;
		loadEmoji(initCategory);
	}

	private View currentSelected;
	private ViewPager myPager;
	private EmojiAdapter adapter;

	@Override
	protected void onCreate() {
		// inflate layout
		LayoutInflater inflater = (LayoutInflater) this.anchor.getContext()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		ViewGroup root = (ViewGroup) inflater.inflate(R.layout.emoji_keyboard,
				null);
		radioSmiley = (RadioButton) root.findViewById(R.id.radioSmiley);
		radioBell = (RadioButton) root.findViewById(R.id.radioBell);
		radioVehicle = (RadioButton) root.findViewById(R.id.radioVehicle);
		radioFlower = (RadioButton) root.findViewById(R.id.radioFlower);
		radioNumber = (RadioButton) root.findViewById(R.id.radioNumber);
		close = (ImageView) root.findViewById(R.id.close);

		radioSmiley.setOnClickListener(this);
		radioBell.setOnClickListener(this);
		radioVehicle.setOnClickListener(this);
		radioFlower.setOnClickListener(this);
		radioNumber.setOnClickListener(this);
		close.setOnClickListener(this);

		currentSelected = radioSmiley;
		mEmojiUtil = ((IRichText) (anchor.getContext())).getEmojiUtil();
		myPager = (ViewPager) root.findViewById(R.id.emoji_pager);
		// set the inflated view as what we want to display
		this.setContentView(root);
	}

	@Override
	public void onClick(View v) {
		// we'll just display a simple toast on a button click
		if (v == close) {
			dismiss();
		} else if (v instanceof Emoji) {
			listener.onEmojiSelected(v);
		} else if (v instanceof RadioButton) {
			if (v != currentSelected) {
				if (v == radioSmiley) {
					currentSelected = radioSmiley;
					loadEmoji(EmojiUtil.SMILEY);
				} else if (v == radioBell) {
					currentSelected = radioBell;
					loadEmoji(EmojiUtil.BELL);
				} else if (v == radioVehicle) {
					currentSelected = radioVehicle;
					loadEmoji(EmojiUtil.VEHICLE);
				} else if (v == radioFlower) {
					currentSelected = radioFlower;
					loadEmoji(EmojiUtil.FLOWER);
				} else if (v == radioNumber) {
					currentSelected = radioNumber;
					loadEmoji(EmojiUtil.NUMBER);
				}
			}
			onSelectedChange();
		}
	}

	private void onSelectedChange() {
		radioSmiley.setBackgroundResource(R.drawable.nonselect);
		radioBell.setBackgroundResource(R.drawable.nonselect);
		radioVehicle.setBackgroundResource(R.drawable.nonselect);
		radioFlower.setBackgroundResource(R.drawable.nonselect);
		radioNumber.setBackgroundResource(R.drawable.nonselect);
		currentSelected.setBackgroundResource(R.drawable.selected);
		((RadioButton) currentSelected).setChecked(true);
	}

	public void loadEmoji(int catagory) {
		ArrayList<Emoji> listEmoji = new ArrayList<Emoji>();
		for (EmojiMap map : mEmojiUtil.getEmojiMaps()[catagory]) {
			Emoji emoji = mEmojiUtil.getEmoji(anchor.getContext(), map.code,
					catagory, false);
			if (!emoji.getEmojiName().equals(EmojiDrawableGetter.NO_IMAGE)) {
				if (!listEmoji.contains(emoji)) {
					emoji.setOnClickListener(this);
					listEmoji.add(emoji);
				}
			}
		}
		if (adapter == null) {
			adapter = new EmojiAdapter(this.anchor.getContext(), listEmoji);
			myPager.setAdapter(adapter);
		} else {
			adapter.setListEmoji(listEmoji);
		}
		myPager.setCurrentItem(0);
		switch (catagory) {
		case EmojiUtil.SMILEY:
			currentSelected = radioSmiley;
			break;
		case EmojiUtil.BELL:
			currentSelected = radioBell;
			break;
		case EmojiUtil.VEHICLE:
			currentSelected = radioVehicle;
			break;
		case EmojiUtil.FLOWER:
			currentSelected = radioFlower;
			break;
		case EmojiUtil.NUMBER:
			currentSelected = radioNumber;
			break;

		default:
			break;
		}
		onSelectedChange();

	}

	/**
	 * Displays like a KeyBoard.
	 * 
	 * @author binhbt
	 * @param xOffset
	 *            offset in the X direction
	 * @param yOffset
	 *            offset in the Y direction
	 */
	public void showLikeKeyBoard() {
		this.preShow();
		// this.window.setFocusable(false);
		// this.window.setTouchable(true);
		// this.window.setOutsideTouchable(false);

		if (isFromTop) {
			this.window.setAnimationStyle(R.style.Animations_GrowFromTop);
			int[] location = new int[2];
			this.anchor.getLocationOnScreen(location);
			this.root.measure(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			// int screenWidth =
			// this.windowManager.getDefaultDisplay().getWidth();
			// int screenHeight =
			// this.windowManager.getDefaultDisplay().getHeight();
			this.window.showAtLocation(this.anchor, Gravity.TOP, 0,
					getStatusBarHeight());
		} else {
			this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
			int[] location = new int[2];
			this.anchor.getLocationOnScreen(location);
			this.root.measure(LayoutParams.WRAP_CONTENT,
					LayoutParams.WRAP_CONTENT);
			// int screenWidth =
			// this.windowManager.getDefaultDisplay().getWidth();
			int screenHeight = this.windowManager.getDefaultDisplay()
					.getHeight();
			this.window.showAtLocation(this.anchor, Gravity.TOP, 0,
					screenHeight);
		}
	}

	public void showFloatKeyBoard() {
		this.preShow();
		// this.window.setFocusable(false);
		// this.window.setTouchable(true);
		// this.window.setOutsideTouchable(false);

		//this.window.setAnimationStyle(R.style.Animations_GrowFromBottom);
		int[] location = new int[2];
		this.anchor.getLocationOnScreen(location);
		this.root.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
		// int screenWidth =
		// this.windowManager.getDefaultDisplay().getWidth();
		int screenHeight = this.windowManager.getDefaultDisplay().getHeight();
		this.window.showAtLocation(this.anchor, Gravity.TOP, 0, screenHeight - this.anchor.getMeasuredHeight() - this.root.getMeasuredHeight());

	}

	public int getStatusBarHeight() {
		Rect rectgle = new Rect();
		Window window = ((Activity) this.anchor.getContext()).getWindow();
		window.getDecorView().getWindowVisibleDisplayFrame(rectgle);
		int StatusBarHeight = rectgle.top;
		//int contentViewTop = window.findViewById(Window.ID_ANDROID_CONTENT)
		//		.getTop();
		//int TitleBarHeight = contentViewTop - StatusBarHeight;
		return StatusBarHeight;
	}

	@Override
	public void afterClose() {
		if (listener != null) {
			listener.afterClose(null);
		}
	}
}